package com.ems.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ems.config.LogPointer;
import com.ems.entity.RoleEntity;
import com.ems.service.IRoleService;
import com.ems.vo.BaseResult;
import com.ems.vo.RoleVo;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author WuZH
 * @since 2023-10-24
 */
@RestController
@RequestMapping ( "/role" )
@CrossOrigin ( value = "*", maxAge = 3600 )
public class RoleController {

    @Resource
    private IRoleService iRoleService;

    @RequestMapping ( "queryList" )
    @LogPointer ( "角色列表" )
    public BaseResult queryList ( @RequestBody RoleVo roleVo ) {
        List<RoleVo> roleVos = iRoleService.queryList ( roleVo );
        return BaseResult.success ( roleVos, iRoleService.queryCount ( roleVo ) );
    }

    @RequestMapping ( "querySingle" )
    @LogPointer ( "查询角色详情" )
    public BaseResult querySingle ( @RequestBody RoleVo roleVo ) {
        RoleVo vo = iRoleService.querySingle ( roleVo );
        return BaseResult.success ( vo );
    }

    @RequestMapping ( "queryRole" )
    @LogPointer ( "获取添加用户的角色" )
    public BaseResult queryRole ( @RequestBody RoleVo roleVo ) {
        List<RoleEntity> roleEntities = iRoleService.getBaseMapper ().selectList ( new QueryWrapper<RoleEntity> ().eq ( "isUsed", 1 ) );
        return BaseResult.success ( roleEntities );
    }

    @RequestMapping ( "saveSingle" )
    @LogPointer ( "增加/修改角色" )
    public BaseResult saveSingle ( @RequestBody RoleVo roleVo ) {
        RoleVo vo = iRoleService.saveSingle ( roleVo );
        return BaseResult.success ( vo );
    }

    @RequestMapping ( "getRoleList" )
    @LogPointer ( "角色列表" )
    public BaseResult getRoleList ( @RequestBody RoleVo roleVo ) {
        List<RoleVo> roleList = iRoleService.getRoleList ( roleVo );
        return BaseResult.success ( roleList );
    }

}
